package com.jiangjiang.eshop.schedule.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.schedule.dao.GoodsAllocationStockMapper;
import com.jiangjiang.eshop.schedule.domain.GoodsAllocationStockEntity;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName GoodsAllocationStockMapper
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/30 22:36
 * @Version 1.0
 **/
@Service
public class ScheduleGoodsAllocationStockService extends ServiceImpl<GoodsAllocationStockMapper, GoodsAllocationStockEntity> {
    /**
     * 根据sku id 查询商品货位库存数据
     * @param goodsAllocationId 商品货位id
     * @param goodsSkuId 商品sku id
     * @return 查询结果
     */
    public GoodsAllocationStockEntity queryBySkuId(Long goodsAllocationId, Long goodsSkuId) {
        LambdaQueryWrapper<GoodsAllocationStockEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsAllocationStockEntity::getGoodsAllocationId, goodsAllocationId)
                .eq(GoodsAllocationStockEntity::getGoodsSkuId, goodsSkuId);
        GoodsAllocationStockEntity entity = this.getOne(wrapper);
        if(Objects.isNull(entity)){
            entity= GoodsAllocationStockEntity.builder()
                    .goodsAllocationId(goodsAllocationId)
                    .goodsSkuId(goodsSkuId)
                    .availableStockQuantity(0L)
                    .lockedStockQuantity(0L)
                    .outputStockQuantity(0L)
                    .build();
            this.save(entity);
        }
        return entity;
    }

    /**
     * 根据sku id 查询所有的货位库存
     * @param goodsSkuId 商品sku id
     * @return 查询结果
     */
    public List<GoodsAllocationStockEntity> queryAllBySkuId(Long goodsSkuId) {
        LambdaQueryWrapper<GoodsAllocationStockEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsAllocationStockEntity::getGoodsSkuId, goodsSkuId);
        return this.list(wrapper);
    }
}
